{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from Mapper import ObjectMapper\n",
    "\n",
    "class Person(object):\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        self.name = 'john'\n",
    "        self.age = 22\n",
    "\n",
    "        #this properties are ignored\n",
    "        self._some_information = 'Not saved in table'\n",
    "        self.__other_stuff = 'Also ignored'\n",
    "\n",
    "p = Person()\n",
    "m = ObjectMapper(p)\n",
    "\n",
    ">>> m.insert()\n",
    "\"INSERT INTO person(age,name) VALUES ('22','john')\"\n",
    ">>> print m.update(id=1)\n",
    "\"UPDATE person SET age = '22', name = 'john' WHERE id_person = '1'\"\n",
    ">>> m.delete(id=1)\n",
    "\"DELETE FROM person WHERE id_person = 1\"\n",
    ">>> m.get_all()\n",
    "\"SELECT age, name FROM person\"\n",
    ">>> m.get_by_id(id=1)\n",
    "\"SELECT age, name FROM person WHERE id_person = 1\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class ObjectMapper(object):\n",
    "\n",
    "    def __init__(self, entity):\n",
    "        self.entity = entity\n",
    "\n",
    "    def _get_pairs(self):\n",
    "        return [(k,str(v)) for k,v in self.entity.__dict__.iteritems() if not k.startswith(\"_\") and not k.startswith(\"__\")]\n",
    "\n",
    "    def _get_names(self):\n",
    "        return [pair[0] for pair in self._get_pairs()]\n",
    "\n",
    "    def _get_values(self):\n",
    "        return [pair[1] for pair in self._get_pairs()]\n",
    "\n",
    "    def _get_names_values(self):\n",
    "        return self._get_names(), self._get_values()\n",
    "\n",
    "    def insert(self):\n",
    "        names, values = self._get_names_values()\n",
    "        names = \"%s\" % \",\".join(names)\n",
    "        values = \"'%s'\" % \"','\".join(values)\n",
    "        return \"INSERT INTO %s(%s) VALUES (%s)\" % (self.get_table(), names, values)\n",
    "\n",
    "    def update(self, id):\n",
    "        pairs = self._get_pairs()\n",
    "        fields = \", \".join([\"%s = '%s'\" % (k,v) for k,v in pairs])\n",
    "        return \"UPDATE %s SET %s WHERE %s = '%s'\" % (self.get_table(), fields, self.get_id(), id)\n",
    "\n",
    "    def delete(self, id):\n",
    "        return \"DELETE FROM %s WHERE %s = %s\" % (self.get_table(), self.get_id(), id)\n",
    "\n",
    "    def get_all(self):\n",
    "        names = \", \".join(self._get_names())\n",
    "        return \"SELECT %s FROM %s\" % (names, self.get_table())\n",
    "\n",
    "    def get_by_id(self, id):\n",
    "        names = \", \".join(self._get_names())\n",
    "        return \"SELECT %s FROM %s WHERE %s = %s\" % (names, self.get_table(), self.get_id(), id)\n",
    "\n",
    "    #Overridables\n",
    "\n",
    "    #You can Extend from this class and override the following methods in order to configurate\n",
    "    #the table name and the id_name\n",
    "\n",
    "    def get_table(self):\n",
    "        return self.entity.__class__.__name__.lower()\n",
    "\n",
    "    def get_id(self):\n",
    "        return \"id_%s\" % self.entity.__class__.__name__.lower()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
